package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.domain.Category;
import org.project.product.android.pfe.fanta.domain.Product;
import org.project.product.android.pfe.fanta.util.Utils;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddProduct extends ActivityParent {

	private Category categorySelected;
	String code;
	private final int idCategory = 1212;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.setContentView(R.layout.add_product);
		categorySelected = (Category) getIntent().getExtras().getSerializable(
		        Utils.CATEGORY_SELECTED);
		code = getIntent().getExtras().getString("code");
		if (categorySelected == null) {
			categorySelected = new Category();
			categorySelected.setId(idCategory);
			categorySelected.setName("Produit Scanne");
			categorySelected.setDescription("Produit scanne");
			DataManagerFacade.getInstance(this).saveCategory(categorySelected,
			        idCategory);
		}
		if (code != null) {
			EditText codeBarre = (EditText) findViewById(R.id.edit_code_barre);
			codeBarre.setText(code);
			codeBarre.setEnabled(false);
		}
	}

	public void addProduct(View view) {

		EditText name = (EditText) findViewById(R.id.edit_name_produit);
		EditText poids = (EditText) findViewById(R.id.edit_product_poids);
		EditText description = (EditText) findViewById(R.id.edit_description);
		EditText codeBarre = (EditText) findViewById(R.id.edit_code_barre);
		EditText quantityField = (EditText) findViewById(R.id.edit_quantity);
		EditText prixField = (EditText) findViewById(R.id.edit_prix);

		if (Utils.checkEditText(getApplicationContext(), name, poids,
		        description, codeBarre, quantityField, prixField)) {

			if (code == null) {
				code = codeBarre.getText().toString();
			}
			String desc = description.getText().toString();
			Product product = new Product();
			product.setCodBarre(code);
			product.setNom(name.getText().toString());
			product.setDescription(desc);

			product.setIdCategory(categorySelected.getId());
			try {
				double poidValue = Double.parseDouble(poids.getText()
				        .toString());
				product.setPoids(poidValue);
				double prix = Double
				        .parseDouble(prixField.getText().toString());
				product.setPrix(prix);
			} catch (NumberFormatException exception) {
				Toast.makeText(this, "Le poids est incorrecte",
				        Toast.LENGTH_LONG).show();
				return;
			}
			int quantity = 0;
			try {
				quantity = Integer.parseInt(quantityField.getText().toString());
			} catch (NumberFormatException exception) {
				exception.printStackTrace();
				quantity = 1;
			}

			product.setQuantity(quantity);
			long number = DataManagerFacade
			        .getInstance(getApplicationContext()).saveProduct(product);
			if (number > 0) {
				Toast.makeText(getApplicationContext(),
				        quantity + "Produit ajoute avec succes",
				        Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(
				        getApplicationContext(),
				        quantity
				                + "Error occuring when processind adding product",
				        Toast.LENGTH_SHORT).show();
			}

			name.setText("");
			poids.setText("");
			description.setText("");
			codeBarre.setText("");
			finish();
		}
	}
}
